package com.wyj.client;

import com.wyj.lib.HelloReply;
import com.wyj.lib.HelloRequest;
import com.wyj.lib.HelloWorldProto;
import com.wyj.lib.MyServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author ：wyj
 * @description：TODO
 * @date ：2022/12/26 11:50 下午
 */
@Component
public class HelloClient {

    private MyServiceGrpc.MyServiceBlockingStub stub;

    //初始化连接 连接到服务端
    @PostConstruct
    public void init() {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9090)
                .usePlaintext()//使用非安全进行传输
                .build();

        //连上服务端
        stub = MyServiceGrpc.newBlockingStub(managedChannel);
    }

    //调用业务端的方法 书写我们的业务逻辑
    public String SayHello(String name) {
        /**  传输我们的参数信息
         *
         * rpc SayHello (HelloRequest) returns (HelloReply)
         *
         * message HelloRequest {
         *     string name = 1;
         * }
         *
         * message HelloReply {
         *     string message = 1;
         * }
         *
         * 这里的话就要设置我们的请求参数
         *
         **/

        HelloRequest request = HelloRequest.newBuilder()
                .setName(name)
                .build();


        HelloReply helloReply = stub.sayHello(request);
        return helloReply.getMessage();
    }

}
